package com.ordertracker.core;

import com.ordertracker.listener.EntityConstraintSupport;
import com.sun.jersey.spi.resource.Singleton;
import org.hibernate.SessionFactory;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.internal.SessionFactoryImpl;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * Description of class.
 * Created 2/22/13 3:09 PM
 *
 * @author andrey.rodin@playtech.com
 */

@Component
public class PersistentEventRegister {

    private SessionFactory sessionFactory;

    @Resource(name = "sessionFactory")
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    
    public void registerListeners(EntityConstraintSupport... listeners) {
        EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry().
                getService(EventListenerRegistry.class);
        registry.getEventListenerGroup(EventType.PRE_INSERT).appendListeners(listeners);
        registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListeners(listeners);
        registry.getEventListenerGroup(EventType.PRE_DELETE).appendListeners(listeners);
        registry.getEventListenerGroup(EventType.PRE_LOAD).appendListeners(listeners);
    }
}
